﻿using System;
using System.Collections;

namespace BackEndProcessorInterface
{
    public class PConfig : IEnumerable
    {
        private readonly Hashtable _config;

        public PConfig()
        {
            _config = new Hashtable();
            Name = "";
            Separation = " ";
        }
        public  PConfig(string sep)
        {
            _config = new Hashtable();
            Name = "";
            Separation = sep; 
        }
        public PConfig(object key, object value)
        {            
          _config = new Hashtable();
            Name = "";
            Separation = " ";
            Add(key, value);
            
        }
        public PConfig(object key, object value,string name)
        {            
          _config = new Hashtable();
          Name = name;
            Separation = " ";
          Add(key, value);
            
        }
        public PConfig(object key, object value, string name,string sep)
        {
            _config = new Hashtable();
            Name = name;
            Separation = sep;
            Add(key, value);

        }
        public IEnumerator GetEnumerator()
        {
            return _config.GetEnumerator(); 
        }
        public void Add(object key, object value)
        {
            if (isExist)
                _config.Add(key, value); 
        }
        public object this[object key]
        {
            get {                              
                return GetValue(key);
            }
        }
        public bool asBool(object key,bool def)
        {
          var o = GetValue(key);
          bool ret;
          int ibool;

            return !Boolean.TryParse(o.ToString(), out ret)
                       ? (Int32.TryParse(o.ToString(), out ibool) ? Convert.ToBoolean(ibool) : def)
                       : ret;
        }

        public string Name { get; set; }
        public string Separation { get; set; }

        public object GetValue(object key)
        {
            return isExist ? _config[key] : null;
        }

        public bool isExist
        {
            get {
                return _config != null;
            }
        }
        public bool isEmpty
        {
            get {
                return _config.Count <= 0;
            }
        }
        public override String ToString()
        {
            var ret = "";
            IEnumerator ienum = _config.GetEnumerator();
            while (ienum.MoveNext())
            {
                var table = (DictionaryEntry)ienum.Current;
                ret += String.Format("{0}{1}{2}",table.Key,Separation,table.Value);
            }
            return ret;
        }
        public string  ValueToString()
        {
            var ret = "";
            IEnumerator ienum = _config.GetEnumerator();
            var i = 0;
            var count = _config.Count;

            while (ienum.MoveNext())
            {
                var table = (DictionaryEntry)ienum.Current;
                if (i < count - 1)
                    ret += String.Format("{0}{1}", table.Value, Separation);
                else
                    ret += table.Value.ToString();
                i++;
            }
            return ret;
        }


    }
}
